package uc108.com.jetpackdemo.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LifecycleObserver;
import androidx.room.Room;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import uc108.com.jetpackdemo.R;
import uc108.com.jetpackdemo.observer.DefaultObserver;
import uc108.com.jetpackdemo.room.AppDatabase;
import uc108.com.jetpackdemo.room.Game;
import uc108.com.jetpackdemo.room.User;

public class MainActivity extends AppCompatActivity {
    AppDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

        getLifecycle().addObserver(new DefaultObserver());

    }

    private void init() {
        database = Room.databaseBuilder(getApplicationContext(), AppDatabase.class,
                "tcydemo-database.db")
                .allowMainThreadQueries()
                .build();
    }

    public void insert(View view) {
//        User u = new User();
//        u.age = 14;
//        u.userName = "Tom";
//        database.userDao().insert(u);

        Game g = new Game();
        g.gameName = new Random().nextInt(100) + "个葫芦娃大战猪八戒";
        g.downloadUrl = "http://www.baidu.com";
        database.gameDao().insert(g);
    }

    public void insertall(View view) {
        User u = new User();
        u.age = 14;
        u.userName = "Carry";

        User u1 = new User();
        u1.age = 114;
        u1.userName = "Carry1";

        User u2 = new User();
        u2.age = 143;
        u2.userName = "Carry2";
        List i = new ArrayList();
        i.add(u);
        i.add(u1);
        i.add(u2);
        database.userDao().insert(i);
//        Log.e("xiaoyi", "insert : " + re);
    }

    public void delete(View view) {
        User u = new User();
        u.age = 14;
        u.userName = "Tom";
        long re = database.userDao().delete(u);
        Log.e("xiaoyi", "delete : " + re);

    }

    public void deleteAll(View view) {
        long re = database.userDao().deleteAll();
        Log.e("xiaoyi", "delete : " + re);
    }

    public void update(View view) {
        User u = new User();
        u.age = 154;
        u.userName = "Change";
        u.userId = 1;
        long re = database.userDao().update(u);
        Log.e("xiaoyi", "update : " + re);

    }

    public void query(View view) {
        int i = 2;
        User u = database.userDao().findUserById(i);
        Log.e("xiaoyi", "user " + u.userName + " age " + u.age);
    }

    public void getAll(View view) {
//        List<User> all = database.userDao().getAllUser();
//        for (User user : all) {
//            Log.e("xiaoyi",user.userName + " id : " + user.userId + "  age: " +user.age);
//        }
        List<Game> games = database.gameDao().getAll();
        for (Game game : games) {
            Log.e("xiaoyi", game.gameName + " url  : " + game.downloadUrl);
        }
    }

    public void toSecond(View view) {
        Intent i = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(i);
    }

    public void toGameList(View view) {
        Intent i = new Intent(MainActivity.this, GameListActivity.class);
        startActivity(i);
    }

    public void toUser(View view) {
        Intent i = new Intent(MainActivity.this, UserActivity.class);
        startActivity(i);
    }

    public void toLifecycle(View view) {
        Intent i = new Intent(this,LifecycleActivity.class);
        startActivity(i);
    }
}
